package com.demomaster.quickcompat.util

import android.os.Handler
import android.os.Looper

object QuickThread {
    //private lateinit var mHandler: Handler
    private val mHandler by lazy { Handler(Looper.getMainLooper()) }
    @JvmStatic
    fun runOnUiThread(runnable: Runnable) {
        if (!isMainThread()) {
            mHandler.post(runnable)
        } else {
            runnable.run()
        }
    }

    /**
     * 运行在子线程
     *
     * @param runnable
     */
    @JvmStatic
    fun runOnSubThread(runnable: Runnable) {
        if (isMainThread()) {
            Thread(runnable).start()
        } else {
            runnable.run()
        }
    }
    @JvmStatic
    fun isMainThread(): Boolean {
        //Thread.currentThread() != Looper.getMainLooper().thread
        return Looper.getMainLooper() == Looper.myLooper()
    }

}